
const state = {
    // 字典：学段学科
    stageRefSubject: [],
    // 包括全学段、全年级属性 9999
    allStageRefSubject: [],
    institutionByBrand: {} // 组织信息
    
};

const getters = {
    stageSubjectObj: (state) => {
        if (!state.stageRefSubject.length) {
            return {};
        }

        let obj = {};
        state.stageRefSubject.forEach(item => {
            obj[item.stage] = item.subjects;
        });
        return obj;
    }
};

const actions = {
    // 获取学段学科数据
    getStageRefSubject({ commit, state, dispatch }, payload = {}) {
        return new Promise((resolve, reject) => {
            if (state.stageRefSubject.length) {
                resolve(state.stageRefSubject);
                return;
            }
            
            G_transmit('listStageRefSubject',
                {
                    queries: {
                        withAllStage: !!payload.withAllStage,
                        withAllSubject: !!payload.withAllSubject,
                        withAllGrade: !!payload.withAllGrade,
                        withMultipleStage: true,
                        // tenant: state.
                    }
                },
            )
                .then((res) => {
                    commit('meta', {
                        stageRefSubject: res.data
                    });
                    resolve(res.data);
                })
                .catch((err) => {
                    reject(err);
                });
        });
    },
    getInstitutionByBrand({
        commit,
        state,
        dispatch
    }) {
        return new Promise((resolve, reject) => {
            if (state.institutionByBrand.length) {
                resolve(state.institutionByBrand);
                return;
            }
            G_transmit('getInstitutionByBrand', {
            }).then((res) => {
                const {status, data} = res;
                commit('meta', {
                    institutionByBrand: data
                })
                resolve(data); 
            })
            .catch((err) => {
                reject(err);
            });
        });
    },
     // 获取学段学科数据
    getAllStageRefSubject({ commit, state, dispatch }, payload) {
        return new Promise((resolve, reject) => {
            if (state.allStageRefSubject.length) {
                resolve(state.allStageRefSubject);
                return;
            }
            G_transmit('listStageRefSubject', {
                queries: {
                    withAllStage: !!payload.withAllStage,
                    withAllSubject: !!payload.withAllSubject,
                    withAllGrade: !!payload.withAllGrade,
                    withMultipleStage: true,
                }
            }).then((res) => {
                commit('meta', {
                    allStageRefSubject: res.data
                });
                resolve(res.data);
            })
            .catch((err) => {
                reject(err);
            });
        });
    },
};

const mutations = {
    meta: (state, data) => {
        Object.assign(state, data);
    }
};

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
};
